/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.kommando.filesystem.core;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Peter De Bruycker
 */
public class FileSource {

    private FileFilter subFolderFileFilter = new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (!includeHiddenFiles && file.isHidden()) {
                return false;
            }

            return file.isDirectory();
        }
    };

    private FileFilter fileFilter = new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (!includeHiddenFiles && file.isHidden()) {
                return false;
            }

            if (types.isEmpty()) {
                return true;
            }

            for (String type : types) {
                if (type.equals("FOLDER") && file.isDirectory()) {
                    return true;
                } else {
                    if (file.getName().toLowerCase().endsWith(type)) {
                        return true;
                    }
                }
            }

            return false;
        }
    };

    private String source;
    private int depth;
    private boolean includeSource;
    private boolean includeHiddenFiles;
    private List<String> types;

    public FileSource(String source, boolean includeSource, boolean includeHiddenFiles, int depth, List<String> types) {
        this.source = source;
        this.includeSource = includeSource;
        this.includeHiddenFiles = includeHiddenFiles;
        this.depth = depth;
        // take a copy to be safe
        this.types = new ArrayList<String>(types);
    }

    public Set<File> getFiles() {
        Set<File> files = new HashSet<File>();

        File root = new File(source);
        if (!root.exists()) {
            // TODO log warning
            return files;
        }

        if (includeSource) {
            files.add(root);
        }

        collectFiles(root, 0, files);

        return files;
    }

    private void collectFiles(File folder, int level, Set<File> results) {
        if (depth >= 0 && depth <= level) {
            return;
        }

        for (File file : folder.listFiles(fileFilter)) {
            results.add(file);
        }

        for (File subFolder : folder.listFiles(subFolderFileFilter)) {
            collectFiles(subFolder, level + 1, results);
        }
    }

    public String getSource() {
        return source;
    }

    public boolean isIncludeSource() {
        return includeSource;
    }

    public boolean isIncludeHiddenFiles() {
        return includeHiddenFiles;
    }

    public int getDepth() {
        return depth;
    }
    
    public List<String> getTypes() {
        return types;
    }
}
